//
//  ZYMainViewController.m
//  KaKaWallet
//
//  Created by admin on 2016/10/24.
//  Copyright © 2016年 ibendi. All rights reserved.
//http://blog.csdn.net/ljh910329/article/details/42421923

#import "ZYMainViewController.h"
#import "MainCollectionViewCell.h"
#import "MainOneCollectionViewCell.h"
#import "MainThreeCollectionViewCell.h"

#import "MainOneCollectionReusableView.h"
#import "MainTwoCollectionReusableView.h"
#import "MainThreeCollectionReusableView.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "ZYChoiceItemView.h"

@interface ZYMainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *mainCollectionView;
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic,strong) NSArray *kkColltionTitltArray;

@end

@implementation ZYMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    [layout layoutAttributesForElementsInRect:CGRectZero];
//    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30)/2, 70);
//    layout.minimumInteritemSpacing = 10; //item 间距
//    layout.minimumLineSpacing = 10;// 行间距
//    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout layoutAttributesForElementsInRect:CGRectZero];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 0;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.delegate = self;
    [self.view addSubview:self.mainCollectionView];
    self.mainCollectionView.backgroundColor = UIColorHex(0xf2f2f2);
    
    [self.mainCollectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"MainCollectionViewCell"];
    [self.mainCollectionView registerClass:[MainOneCollectionViewCell class] forCellWithReuseIdentifier:@"MainOneCollectionViewCell"];
    [self.mainCollectionView registerClass:[MainThreeCollectionViewCell class] forCellWithReuseIdentifier:@"MainThreeCollectionViewCell"];

    [self.mainCollectionView registerClass:[MainOneCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainOneCollectionReusableView"];
    
    
    [self.mainCollectionView registerClass:[MainTwoCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainTwoCollectionReusableView"];
    [self.mainCollectionView registerClass:[MainThreeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainThreeCollectionReusableView"];
    
    self.kkColltionTitltArray = @[@"海景别墅",@"温馨民居",@"精致公寓",@"特色客栈"];

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width)/2-1, 65);
    }else if (indexPath.section ==1) {
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width -30)/2, 228);
    }else{
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, 266);
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 10;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }else if (section == 1){
        return 5;
    }else{
        return 3;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        MainOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainOneCollectionViewCell" forIndexPath:indexPath];
        cell.kkTitleLbl.text = self.kkColltionTitltArray[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else{
        MainThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainThreeCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader){
        if (indexPath.section == 0) {
             MainOneCollectionReusableView* headerViewOne = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainOneCollectionReusableView" forIndexPath:indexPath];
            headerViewOne.kkCollectionAction = ^(NSInteger btnTag){
                NSLog(@"----%ld",btnTag);
                [self goShare];
            };
            headerViewOne.cycleScrollView.clickItemOperationBlock = ^(NSInteger currentIndex){
                NSLog(@"========%ld",currentIndex);
            };

            reusableview = headerViewOne;
            
        }else if(indexPath.section == 1){
            
           MainTwoCollectionReusableView* headerViewTwo = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainTwoCollectionReusableView" forIndexPath:indexPath];
            reusableview = headerViewTwo;

        }else if (indexPath.section == 2){
            
            MainThreeCollectionReusableView* headerViewThree = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MainThreeCollectionReusableView" forIndexPath:indexPath];
            reusableview = headerViewThree;
            headerViewThree.kkAcction = ^{
                NSLog(@"跳转更多");
            };

        }
    }
    return reusableview;
}

//执行的 headerView 代理  返回 headerView 的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, 370);
    }else if (section == 1){
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, 55);
    }else if (section == 2){
        return CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);
    }
    return CGSizeMake(0,0);

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ZYChoiceItemModel *model = [[ZYChoiceItemModel alloc] init];
    model.kkName = @"fsfdsfds";
    
    ZYChoiceItemView *newView = [ZYChoiceItemView shareChoiceView];
    newView.kkDataArray =@[model];
    [newView show];
    
}



-(void)goShare
{
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"icon-address list-normal@3x.png"]];
//    （注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"分享标题"
                                           type:SSDKContentTypeAuto];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       
                       switch (state) {
                           case SSDKResponseStateSuccess:{
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"message:nil
                                                                                  delegate:nil cancelButtonTitle:@"确定"  otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:{
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]  delegate:nil
                                                                     cancelButtonTitle:@"OK"  otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           default:
                               break;
                       }
                   }
         ];}
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
